package school.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import school.domain.Tc;
import school.domain.Tclog;
import school.domain.User;
import school.mapper.TcMapper;
import school.mapper.TclogMapper;
import school.mapper.UserMapper;
import school.support.BaseController;
import school.support.util.ParamUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("tc")
public class TcController extends BaseController {


    @Autowired
    private TcMapper tcMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TclogMapper tclogMapper;

    /**
     * 列表
     *
     * @param tc
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(Tc tc, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        Example example = new Example(Tc.class);
        Example.Criteria criteria = example.createCriteria();
        if (tc.getName() != null) {
            criteria.andLike("name", "%" + tc.getName() + "%");
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Tc> pageInfo = new PageInfo<>(tcMapper.selectByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "tc/list";
    }


    /**
     * 详情
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Tc tc = tcMapper.selectByPrimaryKey(id);
        model.addAttribute(tc);
        return "tc/detail";
    }

    /**
     * 修改
     *
     * @param tc
     * @return
     */
    @RequestMapping("update")
    public String update(Tc tc) {
        tcMapper.updateByPrimaryKeySelective(tc);
        return refresh();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id) {
        tcMapper.deleteByPrimaryKey(id);
        return refresh();
    }

    @GetMapping("add")
    public String add() {
        return "tc/add";
    }

    @PostMapping("add")
    public String add(Tc tc) {
        tcMapper.insertSelective(tc);
        return "redirect:list";
    }

    /**
     * 到更换到处界面
     * @return
     */
    @RequestMapping("change")
    public String change(Model model){
        //当前套餐
        Tc tc = tcMapper.selectByPrimaryKey(sessionUser().getTcid());
        //全部套餐
        List<Tc> tcList = tcMapper.selectAll();
        model.addAttribute(tc);
        model.addAttribute(tcList);
        return "tc/change";
    }

    /**
     * 更换套餐
     * @param id
     * @return
     */
    @RequestMapping("change/id")
    public String change(@PathVariable Integer id,Model model){
        Tc tc = tcMapper.selectByPrimaryKey(id);
        User user = userMapper.selectByPrimaryKey(sessionUser().getId());
        user.setAccount(user.getAccount() - tc.getMoney());
        user.setTcid(tc.getId());
        userMapper.updateByPrimaryKeySelective(user);

        Tclog tclog = new Tclog();
        tclog.setUserid(sessionUser().getId());
        tclog.setTcid(tc.getId());
        tclog.setAddtime(new Date());
        tclogMapper.insertSelective(tclog);
        model.addAttribute("errorMsg","更换成功");
        return refresh();
    }
}
